<template>
  <a-modal
    class=""
    :title="title"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    @ok="handleOk"
    cancelText="关闭">
    <a-row :gutter="20">
      <a-form :form="form">
        <!--<a-col :span="12">
          <a-form-item label="分类">
            <j-category-select v-decorator="['docCategory']" pcode="0" placeholder="请选择分类"/>
            &lt;!&ndash;            <a-input v-decorator="[ 'docCategory']" placeholder="请选择分类"></a-input>&ndash;&gt;
          </a-form-item>
        </a-col>-->
        <!--<a-col :span="12">
          <a-form-item label="文件名称">
            <a-input v-decorator="[ 'docName']" placeholder="请输入文件名称" disabled="true"></a-input>
            &lt;!&ndash;            <a-input @showDocName="updateName"></a-input>&ndash;&gt;
          </a-form-item>
        </a-col>-->
        <a-col :span="12">
          <a-form-item label="制度编号">
            <a-input v-decorator="[ 'docNo']" placeholder="请输入制度编号"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="三甲条款">
            <a-input v-decorator="[ 'jic']" placeholder="请输入三甲条款"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="制度名称">
            <a-input v-decorator="[ 'categoryName']" placeholder="请输入制度名称"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="修改日期">
            <j-date placeholder="请选择修改日期" v-decorator="[ 'upTime']" :trigger-change="true" style="width: 100%"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="制订部门">
            <a-input v-decorator="[ 'dzDepartment']" placeholder="请输入制订部门"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="生效日期">
            <j-date placeholder="请选择生效日期" v-decorator="[ 'sxTime']" :trigger-change="true" style="width: 100%"/>
          </a-form-item>
        </a-col>
        <!--<a-col :span="12">
          <a-form-item label="到期日期">
            <j-date placeholder="请选择到期日期" v-decorator="[ 'dqTime']" :trigger-change="true" style="width: 100%"/>
          </a-form-item>
        </a-col>-->
        <a-col :span="12">
          <a-form-item label="审核人">
            <a-input placeholder="请输入审核人" v-decorator="[ 'audit']"></a-input>
          </a-form-item>
        </a-col>
        <!--<a-col :span="12">
          <a-form-item label="排序">
            <a-input v-decorator="[ 'sort']" placeholder="请输入排序" @change="inputChange"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item>
            <a-checkbox :checked="checkedPrint" @change="checkedPrintClick" v-decorator="[ 'print']">是否允许打印</a-checkbox>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item>
            <a-checkbox :checked="checkedDownload" @change="checkedDownloadClick" v-decorator="[ 'download']">是否允许下载
            </a-checkbox>
          </a-form-item>
        </a-col>-->
        <a-col :span="12">
          <a-form-item label="附件">
            <j-upload ref="uploadFrom" v-decorator="['fileKk']" @changeUrl="updateName"></j-upload>
          </a-form-item>
        </a-col>
      </a-form>
    </a-row>
    <!--    <JUpload ref="uploadFrom"/>-->
  </a-modal>

</template>

<script>
  import JCategorySelect from '@/components/jeecg/JCategorySelect'
  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JEditor from '@/components/jeecg/JEditor'
  import {getAction} from '@/api/manage'
  import {postAction} from '@/api/manage'
  import JUpload from '@/components/jeecg/JUpload'
  import moment from "moment"

  export default {
    name: "EditorModal",
    components: {
      JDate,
      JDictSelectTag,
      JUpload,
      JEditor,
      JCategorySelect
    },
    data() {
      return {
        docCategory:"",
        docName:"",
        count: 0,
        editor: "",
        print: "0",
        download: "0",
        checkedPrint: true,
        checkedDownload: true,
        printShow: false,
        formLayout: 'horizontal',
        form: this.$form.createForm(this),
        title: "操作",
        width: 520,
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {
          docCategory: {
            rules: [
              {required: true, message: '请输入文件分类!'},
            ]
          },
          docName: {
            rules: []
          },
          docNo: {
            rules: []
          },
          dzDepartment: {
            rules: []
          },
          docSize: {
            rules: []
          },
          jic: {
            rules: []
          },
          sxTime: {
            rules: []
          },
          dqTime: {
            rules: []
          },
          isJic: {
            rules: []
          },
          download: {
            rules: []
          },
          print: {
            rules: []
          },
          audit: {
            rules: []
          },
          editor: {
            rules: []
          },
          docStatus: {
            rules: []
          },
          docExpired: {
            rules: []
          },
        },
        url: {
          list: "/doc/editor/list",
          add: "/doc/doc/add",
          addEditor: "/doc/doc/addEditor",
          edit: "/doc/doc/edit",
        }
      }
    },
    methods: {
      inputChange(e) {
        const that = this;
        let percent = /^[0-9]*$/;
        if (!percent.test(e.target.value)) {
          that.$message.warning("请输入数字！")
          this.form.sort = this.form.sort.replace(/[^\d]/g, '')
        }
      },
      showLen(length) {
        this.count = 0
        this.count = length
      },
      updateName(data,sxTime,audit,dzDepartment,updateTime,categoryName,docNo,msg,success) {
        this.docName=data
        this.form.setFieldsValue({
          docName: data,
          sxTime:sxTime,
          audit:audit,
          dzDepartment:dzDepartment,
          upTime:updateTime,
          categoryName:categoryName,
          docNo:docNo
        })
      },
      checkedPrintClick(e) {
        this.checkedPrint = e.target.checked
        if (this.checkedPrint == false) {
          this.$nextTick(() => {
            this.form.setFieldsValue({
              print: "1",
            })
            this.print = 1
          })
          this.printShow = false
        } else {
          this.$nextTick(() => {
            this.form.setFieldsValue({
              print: "0",
            })
            this.print = 0
          })
          this.printShow = true
        }
      },
      checkedDownloadClick(e) {
        this.checkedDownload = e.target.checked
        if (this.checkedDownload == false) {
          this.$nextTick(() => {
            this.form.setFieldsValue({
              download: "1",
            })
            this.download = 1
          })
        } else {
          this.$nextTick(() => {
            this.form.setFieldsValue({
              download: "0",
            })
            this.download = 0
          })
        }
      },
      open(operation) {
        if (operation == 1) {
          getAction(this.url.list, 'get').then((res) => {
            if (res.success) {
              this.temp = res.result.records[0].editor
              this.visible = true;
              this.$nextTick(() => {
                this.form.setFieldsValue({editor: this.temp})
              })

            }
          })
        } else if (operation == 2) {
          this.form.resetFields();
          this.visible = true;
        }
      },
      edit(category) {
        this.docCategory=category.value
        this.form.resetFields();
        this.visible = true;
        this.$nextTick(() => {
          // this.form.setFieldsValue(pick(this.model, id, 'editor'))
          this.form.setFieldsValue({
            docCategory: category.value
          })
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      add() {
        const that = this;
        this.form.validateFields((err, values) => {
          let formData = Object.assign(this.model, values);
          if(formData.upTime!=""&&formData.upTime!=null&&formData.upTime!=undefined&&formData.upTime.length<=10){
            formData.upTime+=" 00:00:00"
          }
          if (formData.sxTime!=null&&formData.sxTime!=""&&formData.sxTime!=undefined&&formData.sxTime.length<=10){
            formData.sxTime+=" 00:00:00"
          }
          formData.docName=this.docName
          formData.docCategory=this.docCategory
          /*var formData={
            editor:this.model
          }*/
          postAction(this.url.addEditor, formData, 'post').then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        })
      },
      //@ok="handleOk"
      handleOk() {
        this.$refs.uploadFrom.changeDisable()
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          values.download = this.download
          values.print = this.print
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
              httpurl += this.url.add;
              method = 'post';
            let formData = Object.assign(this.model, values);
            if(formData.upTime!=""&&formData.upTime!=null&&formData.upTime!=undefined&&formData.upTime.length<=10){
              formData.upTime+=" 00:00:00"
            }
            if (formData.sxTime!=null&&formData.sxTime!=""&&formData.sxTime!=undefined&&formData.sxTime.length<=10){
              formData.sxTime+=" 00:00:00"
            }
            formData.docName=this.docName
            formData.docCategory=this.docCategory
            //console.log(JSON.stringify(formData))
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.$refs.uploadFrom.changeDisable()
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'docCategory', 'docName', 'docNo', 'dzDepartment', 'docSize', 'jic', 'sxTime', 'dqTime', 'isJic', 'download', 'print', 'audit', 'editorString', 'docStatus', 'docExpired'))
      },


    }
  }
</script>